import chapterlib
from datetime import date, timedelta
# import emoji

template = '''☀️心满意足餐厅☀️

📆{date}

🍼每日灵奶:
🍉{dujing1}
🍇{dujing2}

🍱每日配菜:
🌿{daogao1}
🌿{daogao2}
🌿选取当日读的章节中有感动的经文DG

🕊️自由选择两项搭配🕊️

🍷营养丰富，请勿错过🍷'''


def parse_text(text_raw):

    options = ('周一', '周二', '周三', '周四', '周五', '周六', '周日')
    d = dict()
    key = 0
    values = []
    for line in text_raw.splitlines():
        line = line.strip()
        if line:
            if line in options:
                # 读取到新key就储存上次的k:v
                # if key的作用是排除首次读取到的key,那时上次的k:v为空值
                # 也可以用if values,但因为程序逻辑变更,须存储所有用户输入的key,即使value为空
                if key:
                    d[key] = values.copy()
                    values.clear()
                key = week_info_to_int(line)
            else:
                values.append(line)
    # 除了排除首次空k:v,还要追加最后一次k:v
    if key:
        d[key] = values.copy()
        values.clear()
    return d


def get_dates(weekcode: int):

    today = date.today()
    init_day = None
    match weekcode:
        case 0:
            # 上一周
            days = today.isoweekday()-1+7
            td = timedelta(days=days)
            init_day = today - td
        case 1:
            days = today.isoweekday()-1
            td = timedelta(days=days)
            init_day = today - td
        case 2:
            days = 7-today.isoweekday()+1
            td = timedelta(days=days)
            init_day = today + td

    oneday = timedelta(days=1)
    day_list = [init_day]
    for i in range(6):
        init_day += oneday
        day_list.append(init_day)
    return day_list


def get_text(dujing1, dujing2, weekcode, daogao_uri):
    dates = [get_date_info(d) for d in get_dates(weekcode)]
    with open(daogao_uri, 'r', encoding='utf-8') as f:
        daogao_schedule = f.read()
    daogao_set = parse_text(daogao_schedule)
    daogao1 = [(daogao_set.get(i)[0] or '') for i in sorted(daogao_set.keys())]
    daogao2 = [(daogao_set.get(i)[1] or '') for i in sorted(daogao_set.keys())]
    *start1, capacity1 = dujing1
    *start2, capacity2 = dujing2
    cg1 = chapterlib.chapter_generator(start1, capacity1)
    cg2 = chapterlib.chapter_generator(start2, capacity2)

    args_list = list(zip(dates, cg1, cg2, daogao1, daogao2))
    for i in range(7):
        a, b, c, d, e = args_list[i]
        with open(f'星期{i+1}.txt', 'w', encoding='utf-8') as f:
            plaintext = template.format(
                date=a, dujing1=b, dujing2=c, daogao1=d, daogao2=e)
            # emojitext=emoji.emojize(plaintext)
            f.write(plaintext)


def week_info_to_int(week):
    week_dict = {'周一': 1, '周二': 2, '周三': 3,
                 '周四': 4, '周五': 5, '周六': 6, '周日': 7}
    return week_dict.get(week)


def get_week_info(_date):
    week_dict = {1: '周一', 2: '周二', 3: '周三',
                 4: '周四', 5: '周五', 6: '周六', 7: '周日'}
    return week_dict.get(_date.isoweekday())


def get_date_info(_date):
    return f'{_date.month:02}月{_date.day:02}日 {get_week_info(_date)}'


if __name__ == '__main__':
    # 参数1: 卷名, 参数2: 章, 参数3: 每日读经章数
    dujing1 = '创世记', 1, 2
    dujing2 = '启示录', 2, 1
    # 0:上周, 1:本周, 2:下周
    weekcode = 1
    res = 'weekly_schedule.txt'
    get_text(dujing1, dujing2, weekcode, res)
